﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web;
using System.Configuration;
using CYP.IISMonitor.Utility.Log;

namespace CYP.IISMonitor.Utility
{
    /// <summary>
    /// 配置文件助手
    /// </summary>
    public static class ConfigHelper
    {
        /// <summary>
        /// 连接池
        /// </summary>
        private static Hashtable _connPool;

        /// <summary>
        /// 日志记录
        /// </summary>
        private static readonly TextLogger _tracing = TextLogManager.Create(typeof(ConfigHelper));

        /// <summary>
        /// 获取app.config里配置项
        /// </summary>
        /// <param name="configName">key</param>
        /// <returns>配置值</returns>
        public static string GetConfig(string configName)
        {
            string val = string.Empty;
            if (string.IsNullOrEmpty(val))
            {
                val = System.Configuration.ConfigurationManager.AppSettings[configName];
                //_tracing.Info("{0} value:{1}.", configName, val);
                if (val == null)
                {
                    _tracing.Error("App.Config node {0} is not exists.", configName);
                    return string.Empty;
                }
            }

            return val;
        }

        /// <summary>
        /// 从配置文件中获取连接字符串
        /// </summary>
        /// <param name="key">连接字符换键值</param>
        /// <returns>连接字符串</returns>
        public static string GetConnectionString(string key)
        {
            if (_connPool == null)
            {
                _connPool = new Hashtable();
            }

            if (_connPool.ContainsKey(key))
            {
                return _connPool[key].ToString();
            }

            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings[key].ConnectionString;
            _connPool[key] = connStr;
            return connStr;
        }
    }
}
